<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      v-model="visibleDialog"
      title="故障部位选择"
      style="min-width: 880px"
      draggable
      align-center
      @close="closeDialog"
      destroy-on-close
      class="selectDialog"
    >
      <div class="search-container">
        <el-form :model="searchParam" :inline="true">
          <el-form-item label="设备种类" prop="assetTypeId">
            <el-tree-select
              placeholder="请选择设备种类"
              v-model="searchParam.assetTypeId"
              :data="deviceTypeList"
              node-key="id"
              :props="{
                label: 'assetTypeName',
                value: 'id',
                children: 'nodes',
              }"
              :disabled="props.assetTypeId !== ''"
              :render-after-expand="false"
              :check-strictly="true"
              style="width: 100%"
              clearable
            />
            <!-- <el-select
              v-model="searchParam.assetTypeId"
              placeholder="请选择设备种类"
              clearable
              filterable
            >
              <el-option
                v-for="item in deviceTypeList"
                :key="`${item.id}`"
                :label="`${item.assetTypeName}`"
                :value="`${item.id}`"
              />
            </el-select> -->
          </el-form-item>
          <el-form-item label="部位名称" prop="positionName">
            <el-input
              v-model="searchParam.positionName"
              placeholder="请输入部位名称"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search"
              ><i-ep-search />搜索</el-button
            >
            <el-button @click="resetHandle"><i-ep-refresh />重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-card shadow="never" class="table-container">
        <div class="formTable">
          <el-table
            ref="singleTableRef"
            :data="tableData"
            style="width: 100%"
            :max-height="`calc(100vh - 400px)`"
            highlight-current-row
            default-expand-all
            @current-change="handleCurrentChange1"
            border
            row-key="id"
            :indent="30"
            :tree-props="{
              children: 'nodes',
              hasChildren: 'hasChildren',
            }"
          >
            <el-table-column
              label="部位编号"
              prop="positionCode"
              min-width="150"
              show-overflow-tooltip
            />
            <el-table-column
              label="部位名称"
              prop="positionName"
              min-width="200"
              show-overflow-tooltip
            />
          </el-table>
        </div>
      </el-card>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" :loading="loading" @click="handleSubmit"
            >确 定
          </el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import { ElMessage, ElForm } from "element-plus";
import { ref, reactive, watch, onBeforeUnmount } from "vue";
import request from "@/utils/request";
defineOptions({
  name: "EamFaultMeasureSelect",
  inheritAttrs: false,
});

const emit = defineEmits(["change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  // 设备种类id
  assetTypeId: {
    type: String,
    default: () => "",
  },
  // 资产大类
  assetKind: {
    type: Number,
    default: () => 1,
  },
});

//获取表格数据 api 方法
const requestApi = "/eam/eamassetposition/getTree";
//获取数据初始化参数
const initParam = {};
// 初始化搜索参数数据
// 初始化搜索参数数据
const searchParamInit = {
  assetTypeId: props.assetTypeId,
  positionCode: "",
  positionName: "",
};
// 是否需要分页
const pagination = true;
// 无需二次数据处理&不传赞数也行
const dataCallBack = (data: any) => {
  return data;
};
// const dataCallBack = ref(undefined);
// 数据请求错误后处理
function requestError() {}
//删除
const deleteParam = {
  api: "",
  data: {},
};

const { tableData, searchParam, search, getTableList, loading }: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack,
  requestError,
  deleteParam
);

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    if (newVal) {
      (searchParam.value.assetTypeId = props.assetTypeId), getDeviceTypeList();
      getTableList();
    }
  }
);

// 选择高亮
const singleTableRef = ref();
const currentRow = ref();
const handleCurrentChange1 = (val: any) => {
  currentRow.value = val;
};

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
}

/** email消息发送记录保存提交 */
function handleSubmit() {
  if (currentRow.value) {
    emit("change-visible", false, currentRow.value);
  } else {
    return ElMessage.warning("请选择故障部位");
  }
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

onMounted(() => {});

function resetHandle() {
  searchParam.value.assetTypeId = props.assetTypeId;
  search();
  // reset();
}

// 获取故障原因id
const deviceTypeList = ref<any>([]);
async function getDeviceTypeList() {
  await request({
    url: "/eam/equAssetType/getTree",
    method: "post",
    data: { assetKind: props.assetKind },
  })
    .then((res) => {
      deviceTypeList.value = res.data || [];
    })
    .finally(() => {});
}
</script>

<style scoped lang="scss">
:deep(.selectDialog .el-dialog__body) {
  padding: 20px;
  max-height: 70vh;
  overflow-y: auto;
  padding: 20px;
}
.progress {
  color: red;
}
.success {
  color: rgb(100, 207, 86);
}
</style>
